/**
 * author: zhangwei
 * 角色新增、编辑
 */
var pageType = "add";
var main = {

    init: function() {
        pageType = localStorage.doctorAppointmentType;


        this.initTable(pageType);
        if(pageType == 'edit') {
            $('#submit').text('保存');
            // this.getDetailData();
            this.id = localStorage.doctorAppointmentId;
            loadData(this.id);
        }

    },
    initTable: function(pageType) {
        var cyProps = "url:'"+"/org/queryOrgListTree',name:'orgId'";
        $("#orgId").attr("cyProps",cyProps);
        layui.use(['form','laydate'], function() {
            var form = layui.form;
            if(pageType != "edit"){
                setUserSelect();
                setDoctorSelect(-1);
                setAppointmentDetailTimeSelect();
                form.render('select')
            }

            var laydate = layui.laydate;

            function formatminutes(date){
                var aa = $(".laydate-time-list li ol")[1];
                var showtime = $($(".laydate-time-list li ol")[1]).find("li");
                for (var i = 0; i < showtime.length; i++) {
                    var t00 = showtime[i].innerText;
                    if (t00 != "00" && t00 != "30") {
                        showtime[i].hidden = true;
                    }
                }
                $($(".laydate-time-list li ol")[2]).find("li").remove();  //清空秒

            }

            //执行一个laydate实例
            laydate.render({
                elem: '#test1' //指定元素
                ,min:getNowFormatDate()
            });


            //监听提交
            form.on('submit(formSubmit)', function(data) {
                var url = "/doctorAppointment/saveDoctorAppointment";
                var json = $("#roleForm").serialize();
                $.ajax({
                    type:"post",
                    data:json,
                    async:false,
                    url:url,
                    success:function(result) {
                        if (result.code == property.successCode) {
                            if(pageType == "edit"){
                                successMsg("修改成功");
                            }else{
                                successMsg("添加成功");
                            }
                            parent.$t.goback("/doctorAppointment/manage");
                        } else{
                            errorMsg(result.msg);
                        }
                    },
                    error:function(result) {
                        errorMsg("系统异常");
                    }
                });
                return false;
            });

            //监听重置
            $("#cancel").click(function () {
                parent.$(".myRefresh").click();
                layer.close(index);
                return false;
            });

        });
    },
    getDetailData: function() {
        layui.use('form', function() {
            var form = layui.form;
            form.val("myform", {
                "title": "贤心"
                ,"name": "vvvvv"
                ,"city": "1"  // 下拉框初始赋值
                ,"desc": "我爱layui"
            });
        });
    }
}
main.init();

/**
 * 加载表单数据
 * @param id 角色id
 */
function loadData(id) {
    this.type = "edit";
    layui.use('form', function(){
        setUserSelect();
        setDoctorSelect(-1);
        setAppointmentDetailTimeSelect();
        var form = layui.form;
        var index = parent.layer.getFrameIndex(window.name);
        var json = {"id":id};
        //加载数据
        $.ajax({
            type:"get",
            data:json,
            async:false,
            url:"/doctorAppointment/getDoctorAppointment",
            success:function(result) {
                if (result.code == property.successCode) {
                    setFormData(result.data);
                    form.render('select');
                } else {
                    errorMsg(result.msg);
                }
            },
            error:function(result) {
                errorMsg("系统异常");
            }
        });
    });


}

/**
 * 设置表单数据
 * @param data
 */
function setFormData(data) {
    data['appointmentTime'] = data['appointmentTimeStr']
    property.setForm($("#roleForm"),data);


}

function setUserSelect() {
    htmlstr = "<option value=''>请选择</option>"
    data =  getUserList();
    for(i=0;i<data.length;i++){
        item = data[i]
        str = "<option value="+item['id']+">"+item['userName']+"</option>"
        htmlstr = htmlstr+str
    }
    $("#patientId").html(htmlstr)
}

function setDoctorSelect(orgId) {
    htmlstr = "<option value=''>请选择</option>"
    data =  getDoctorList(orgId);
    for(i=0;i<data.length;i++){
        item = data[i]
        str = "<option value="+item['doctorCode']+">"+item['doctorName']+"</option>"
        htmlstr = htmlstr+str
    }
    $("#doctorCode").html(htmlstr)
}

function setAppointmentDetailTimeSelect() {
    htmlstr = "<option value=''>请选择</option>"
    data =  getDetailTimeList();
    for(i=0;i<data.length;i++){
        item = data[i]
        str = "<option value="+item+">"+item+"</option>"
        htmlstr = htmlstr+str
    }
    $("#appointmentDetailTime").html(htmlstr)
}


function getUserList() {
    data = null
    $.ajax({
        type:"get",
        async:false,
        url:"/user/getUserList",
        success:function(result) {
            if (result.code == property.successCode) {
                data = result.data
            } else {
                errorMsg(result.msg);
            }
        },
        error:function(result) {
            errorMsg("系统异常");
        }
    });
    return data
}

function getDoctorList(orgId) {
    data = null
    $.ajax({
        type:"get",
        async:false,
        url:"/doctor/getDoctorList?orgId="+orgId,
        success:function(result) {
            if (result.code == property.successCode) {
                data = result.data;
            } else {
                errorMsg(result.msg);
            }
        },
        error:function(result) {
            errorMsg("系统异常");
        }
    });
    return data;
}


function getDetailTimeList() {
    data = null
    $.ajax({
        type:"get",
        async:false,
        url:"/doctor/getDetailTimeList",
        success:function(result) {
            if (result.code == property.successCode) {
                data = result.data;
            } else {
                errorMsg(result.msg);
            }
        },
        error:function(result) {
            errorMsg("系统异常");
        }
    });
    return data;
}






